<template>
  <div>
    <el-tabs :tab-position="tabPosition" v-model="activeName" @tab-click="handleClick">
      <el-tab-pane label="过磅通知单" name="first">
        <section>
          <el-form
            ref="form"
            :model="queryForm"
            label-width="76px"
            :inline="true"
            class="queryForm"
          >
            <el-form-item label="关键字:" label-width="82px">
              <div style="width: 200px">
                <el-input
                  v-model="queryForm.keyword"
                  size="small"
                  placeholder="请输入模板编号/模板名称查询"
                ></el-input>
              </div>
            </el-form-item>
            <el-form-item>
              <div class="but-container" style="margin-left: 35px">
                <el-button
                  type="primary"
                  size="small"
                  style="margin-right:5px"
                  @click="
                getList(
                  page2,
                  orderBy.name,
                  orderBy.method,
                  queryForm.keyword,
                  2
                )"
                >查询</el-button>
                <el-button size="small" @click="reset">重置</el-button>
              </div>
            </el-form-item>
          </el-form>
        </section>
        <section>
          <el-button
            style="margin-left: 15px;margin-right:5px"
            type="primary"
            size="small"
            @click="add"
            v-if="hasPermission('basis:weightTemplate:add')"
          >新增</el-button
          >
            <section class="right-but" style="display:flex">
                <el-button type="text" style="margin-right:-13px"  @click="refreshList()">
                    <img style="width:44px;height:36px;" src="~@/assets/img/shuaxin.png" >
                </el-button>
                <PopoverBut :checkedProps="checkedProps"  :options="options" @getCheckeds="handlerCloumn"></PopoverBut>
            </section>
        </section>
        <el-table
          size="mini"
          v-loading="loading"
          :data="tableData2"
          style="width: 100%; margin-top: 20px"
          border
          :header-cell-style="{
        'background-color': '#eeeeee !important',
      }"
          :row-class-name="tableRowClassName2"
          @sort-change="sortthiscolumn2"
        >
          <el-table-column type="index" label="序号" width="70" align="center">
            <template slot-scope="scope">
          <span>{{
            (page2.pageNo - 1) * page2.pageSize + scope.$index + 1
          }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="processType" label="业务流程类型" width="90px" align="center">
            <template slot-scope="scope">
              {{$dictUtils.getDictLabel("processType", scope.row.processType, "")}}
            </template>
          </el-table-column>
          <el-table-column prop="templateCode" label="模板编号" sortable="template_code"> </el-table-column>
          <el-table-column prop="templateName" label="模板名称" sortable="template_name"> </el-table-column>
          <el-table-column prop="describes" label="描述"> </el-table-column>
          <el-table-column prop="state" label="状态" width="90px" align="center" sortable="state">
            <template slot-scope="scope">
              {{$dictUtils.getDictLabel("state", scope.row.state, "")}}
            </template>
          </el-table-column>
            <span v-for="(item,index) in checkedProps" :key="index">
             <el-table-column  prop="createBy.loginName" :label="item" v-if="item==='创建人'" min-width="120"></el-table-column>
             <el-table-column  prop="createDate" :label="item" v-if="item==='创建时间'"  min-width="180"></el-table-column>
             <el-table-column  prop="updateBy.loginName" :label="item" v-if="item==='上次操作人'" min-width="140"></el-table-column>
             <el-table-column  prop="updateDate" :label="item" v-if="item==='上次操作时间'"  min-width="180" sortable="update_date"></el-table-column>
          </span>
          <el-table-column prop="adddatas" label="操作" width="218px">
            <template slot-scope="scope">
              <span style="padding-right: 6px"></span>
              <el-link type="primary" :underline="false" @click="edit(scope.row)"
                       v-if="hasPermission('basis:weightTemplate::edit')"
                       :disabled="scope.row.state ===1"
              >修改</el-link
              >|
              <el-link type="primary" :underline="false" @click="delRegiste(scope.row)"
                       v-if="hasPermission('basis:weightTemplate:del')"
                       :disabled="scope.row.state ===1"
              >删除</el-link
              >|
              <el-link type="primary" :underline="false" @click="enableVal(scope.row)"
                       v-if="scope.row.state === 0 && hasPermission('basis:weightTemplate:modifyState')"
              >启用</el-link
              >
              <el-link type="primary" :underline="false" @click="disableVal(scope.row)"
                       v-if="scope.row.state === 1 && hasPermission('basis:weightTemplate:modifyState')"
              >停用</el-link
              >
              <span style="padding-right: 20px"></span>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          background
          @size-change="handleSizeChange2"
          @current-change="handleCurrentChange2"
          :page-size="page2.pageSize"
          :current-page="page2.pageNo"
          layout="total,sizes,prev, pager, next, jumper"
          :total="page2.total"
        >
        </el-pagination>
      </el-tab-pane>
      <el-tab-pane label="过磅单" name="second">
        <section>
          <el-form
            ref="form"
            :model="queryForm"
            label-width="76px"
            :inline="true"
            class="queryForm"
          >
            <el-form-item label="关键字:" label-width="82px">
              <div style="width: 200px">
                <el-input
                  v-model="queryForm.keyword"
                  size="small"
                  placeholder="请输入模板编号/模板名称查询"
                ></el-input>
              </div>
            </el-form-item>
            <el-form-item>
              <div class="but-container" style="margin-left: 35px">
                <el-button
                  type="primary"
                  size="small"
                  style="margin-right:5px"
                  @click="
                getList(
                  page,
                  orderBy.name,
                  orderBy.method,
                  queryForm.keyword,
                  1
                )"
                >查询</el-button>
                <el-button size="small" @click="reset">重置</el-button>
              </div>
            </el-form-item>
          </el-form>
        </section>
        <section>
          <el-button
            style="margin-left: 15px;margin-right:5px"
            type="primary"
            size="small"
            @click="add"
            v-if="hasPermission('basis:weightTemplate:add')"
          >新增</el-button
          >
            <section class="right-but" style="display:flex">
                <el-button type="text" style="margin-right:-13px"  @click="refreshList()">
                    <img style="width:44px;height:36px;" src="~@/assets/img/shuaxin.png" >
                </el-button>
                <PopoverBut :checkedProps="checkedProps2"  :options="options" @getCheckeds="handlerCloumn2"></PopoverBut>
            </section>
        </section>
        <el-table
          size="mini"
          v-loading="loading"
          :data="tableData"
          style="width: 100%; margin-top: 20px"
          border
          :header-cell-style="{
        'background-color': '#eeeeee !important',
      }"
          :row-class-name="tableRowClassName"
          @sort-change="sortthiscolumn"
        >
          <el-table-column type="index" label="序号" width="70" align="center">
            <template slot-scope="scope">
          <span>{{
            (page.pageNo - 1) * page.pageSize + scope.$index + 1
          }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="processType" label="业务流程类型" width="90px" align="center">
            <template slot-scope="scope">
              {{$dictUtils.getDictLabel("processType", scope.row.processType, "")}}
            </template>
          </el-table-column>
          <el-table-column prop="templateCode" label="模板编号" sortable="template_code"> </el-table-column>
          <el-table-column prop="templateName" label="模板名称" sortable="template_name"> </el-table-column>
          <el-table-column prop="describes" label="描述"> </el-table-column>
          <el-table-column prop="state" label="状态" width="90px" align="center" sortable="state">
            <template slot-scope="scope">
              {{$dictUtils.getDictLabel("state", scope.row.state, "")}}
            </template>
          </el-table-column>
            <span v-for="(item,index) in checkedProps2" :key="index">
             <el-table-column  prop="createBy.loginName" :label="item" v-if="item==='创建人'" min-width="120"></el-table-column>
             <el-table-column  prop="createDate" :label="item" v-if="item==='创建时间'"  min-width="180"></el-table-column>
             <el-table-column  prop="updateBy.loginName" :label="item" v-if="item==='上次操作人'" min-width="140"></el-table-column>
             <el-table-column  prop="updateDate" :label="item" v-if="item==='上次操作时间'"  min-width="180" sortable="update_date"></el-table-column>
          </span>
          <el-table-column prop="adddatas" label="操作" width="218px">
            <template slot-scope="scope">
              <span style="padding-right: 6px"></span>
              <el-link type="primary" :underline="false" @click="edit(scope.row)"
                       v-if="hasPermission('basis:weightTemplate::edit')"
                       :disabled="scope.row.state ===1"
              >修改</el-link
              >|
              <el-link type="primary" :underline="false" @click="delRegiste(scope.row)"
                       v-if="hasPermission('basis:weightTemplate:del')"
                       :disabled="scope.row.state ===1"
              >删除</el-link
              >|
              <el-link type="primary" :underline="false" @click="enableVal(scope.row)"
                       v-if="scope.row.state === 0 && hasPermission('basis:weightTemplate:modifyState')"
              >启用</el-link
              >
              <el-link type="primary" :underline="false" @click="disableVal(scope.row)"
                       v-if="scope.row.state === 1 && hasPermission('basis:weightTemplate:modifyState')"
              >停用</el-link
              >
              <span style="padding-right: 20px"></span>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :page-size="page.pageSize"
          :current-page="page.pageNo"
          layout="total,sizes,prev, pager, next, jumper"
          :total="page.total"
        >
        </el-pagination>
      </el-tab-pane>
    </el-tabs>
    <EditForm
      ref="editForm"
      @refreshDataList="refreshList"
    ></EditForm>
  </div>
</template>
<script>
import { getList, delRegiste, modifyState } from '@/api/basis/weightTemple/weightTemple.js'
import EditForm from './editForm'
import PopoverBut from '@/components/button/popoverBut'
const options1 = []
const options2 = []
export default {
  components: {
      PopoverBut,
    EditForm
  },
  data () {
    return {
      tablesIndex: '',
      activeName: 'first',
      queryForm: {
        keyword: ''
      },
      page: {
        pageSize: 10,
        pageNo: 1,
        total: 0
      },
      page2: {
        pageSize: 10,
        pageNo: 1,
        total: 0
      },
      ids: '',
      tabPosition: 'left',
      tableData: [],
      tableData2: [],
      selectDatas: [],
        checkedProps: options1,
        checkedProps2: options2,
        options: [
            {
                label: '创建人',
                key: 'createBy.loginName'
            },
            {
                label: '创建时间',
                key: 'createDate'
            },
            {
                label: '上次操作人',
                key: 'updateBy.loginName'
            },
            {
                label: '上次操作时间',
                key: 'updateDate'
            }],
      orderBy: {
        name: 'create_date',
        method: 'desc'
      },
      loading: false
    }
  },
  methods: {
    // 获取列表带分页
    getList (params, orderName, orderByAORC, keyword, templateType) {
      this.loading = true
      getList({
        pageSize: params.pageSize,
        pageNo: params.pageNo,
        orderBy: orderName,
        orderByAORC: orderByAORC,
        keyword: keyword,
        templateType: templateType
      }).then(({data}) => {
        if (data.code === '200') {
          if (this.tablesIndex === '0') {
            this.tableData2 = data.result.list
            this.page2.total = data.result.count
            this.loading = false
          } else {
            this.tableData = data.result.list
            this.page.total = data.result.count
            this.loading = false
          }
        }
      })
    },
      // 是否显示某列数据
      handlerCloumn (e) {
          this.checkedProps = e
      },
      // 是否显示某列数据
      handlerCloumn2 (e) {
          this.checkedProps2 = e
      },
    refreshList () {
      if (this.tablesIndex === '0') {
        this.getList(this.page2, 'create_date', 'desc', this.queryForm.keyword, 2)
      } else {
        this.getList(this.page, 'create_date', 'desc', this.queryForm.keyword, 1)
      }
    },
    // 重置查询条件
    reset () {
      this.queryForm = {}
    },
    edit (row) {
      this.$refs.editForm.init('edit', row.id)
    },
    add () {
      this.$refs.editForm.init('add')
    },
    delRegiste (row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then((data) => {
        delRegiste({
          ids: row.id
        }).then(({data}) => {
          if (data.code === '200') {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            if (this.tablesIndex === '0') {
              this.getList(this.page2, 'create_date', 'desc', this.queryForm.keyword, 2)
            } else {
              this.getList(this.page, 'create_date', 'desc', this.queryForm.keyword, 1)
            }
          }
        })
      })
    },
    enableVal (row) {
      modifyState({
        id: row.id,
        state: 1
      }).then(({data}) => {
        if (data.code === '200') {
          this.$message({
            type: 'success',
            message: '启用成功!'
          })
          if (this.tablesIndex === '0') {
            this.getList(this.page2, 'create_date', 'desc', this.queryForm.keyword, 2)
          } else {
            this.getList(this.page, 'create_date', 'desc', this.queryForm.keyword, 1)
          }
        }
      })
    },
    disableVal (row) {
      modifyState({
        id: row.id,
        state: 0
      }).then(({data}) => {
        if (data.code === '200') {
          this.$message({
            type: 'success',
            message: '停用成功!'
          })
          if (this.tablesIndex === '0') {
            this.getList(this.page2, 'create_date', 'desc', this.queryForm.keyword, 2)
          } else {
            this.getList(this.page, 'create_date', 'desc', this.queryForm.keyword, 1)
          }
        }
      })
    },
    handleClick (e) {
      this.tablesIndex = e.index
      if (e.index === '0') {
        this.getList(this.page2, 'create_date', 'desc', this.queryForm.keyword, 2)
      } else {
        this.getList(this.page, 'create_date', 'desc', this.queryForm.keyword, 1)
      }
    },
    // 卡类型排序
    sortthiscolumn (a) {
      this.orderBy.name = a.prop
      this.page.pageNo = 1
      if (a.order === 'ascending') {
        this.orderBy.method = 'asc'
      } else {
        this.orderBy.method = 'desc'
      }
      this.getList(this.page, this.orderBy.name, this.orderBy.method, this.queryForm.keyword, 1)
    },
    handleSizeChange (e) {
      this.page.pageSize=e
      this.getList(this.page, 'create_date', 'desc', this.queryForm.keyword, 1)
    },
    handleCurrentChange (e) {
      this.page.pageNo = e
      this.getList(this.page, 'create_date', 'desc', this.queryForm.keyword, 1)
    },
    sortthiscolumn2 (a) {
      this.orderBy.name = a.prop
      this.page.pageNo = 1
      if (a.order === 'ascending') {
        this.orderBy.method = 'asc'
      } else {
        this.orderBy.method = 'desc'
      }
      this.getList(this.page2, this.orderBy.name, this.orderBy.method, this.queryForm.keyword, 2)
    },
    handleSizeChange2 (e) {
      this.page2.pageSize=e
      this.getList(this.page2, 'create_date', 'desc', this.queryForm.keyword, 2)
    },
    handleCurrentChange2 (e) {
      this.page2.pageNo = e
      this.getList(this.page2, 'create_date', 'desc', this.queryForm.keyword, 2)
    },
    tableRowClassName ({ row, rowIndex }) {
      if (rowIndex % 2 === 1) {
        return 'warning-row'
      } else {
        return 'success-row'
      }
    },
    tableRowClassName2 ({ row, rowIndex }) {
      if (rowIndex % 2 === 1) {
        return 'warning-row'
      } else {
        return 'success-row'
      }
    }
  },
  mounted () {
    this.tablesIndex = '0'
    this.getList(this.page2, 'create_date', 'desc', this.queryForm.keyword, 2)
  }
}
</script>
<style lang="scss" scoped>
.el-table .success-row {
  background: #f0f9eb;
}
::v-deep .el-table .warning-row {
  background: #f7f7f7;
}
</style>
